package co.chuangdian.oflauncher.folders.setting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import cn.onekit.CALLBACK;
import cn.onekit.CONFIG;
import cn.onekit.DIALOG;
import cn.onekit.JSON;
import cn.onekit.android.ACTIVITY;
import co.chuangdian.oflauncher.R;
import uc.Setting;
import uc.SettingGroup;
import uc.SettingListView;

public class ManageFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.foldersetting_manage, container, false);
	}

	Map<String, Integer> indexes = new HashMap<String, Integer>();
	SettingGroup[] settingGroups;
	JSONArray folders;
	SettingListView settingListView;

	JSONArray array = new JSONArray();

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		settingListView = (SettingListView) getActivity().findViewById(R.id.settingListView);
		ImageView addButton = (ImageView) getActivity().findViewById(R.id.addButton);
		addButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 5-19
				Intent intent = new Intent(getActivity(), AddFolderActivity.class);
				// Intent intent = new Intent(getActivity(), MainUI.class);

				startActivity(intent);
			}
		});
		//
		folders = JSON.parses((String) CONFIG.get("folders"));
		reloadData();
	}

	@SuppressWarnings("deprecation")
	Drawable geticon(JSONObject folder_icon) {
		Drawable drawable = null;
		if (folder_icon.optBoolean("system")) {
			switch (folder_icon.optInt("id")) {
			case 1:
				drawable = getActivity().getResources().getDrawable(R.drawable.icon_music_grey);
				break;
			case 2:
				drawable = getActivity().getResources().getDrawable(R.drawable.icon_camera_grey);
				break;
			case 3:
				drawable = getActivity().getResources().getDrawable(R.drawable.icon_travel_grey);
				break;
			case 4:
				drawable = getActivity().getResources().getDrawable(R.drawable.icon_daily_grey);
				break;
			default:
				break;
			}
		} else {
			drawable = ((ACTIVITY) getActivity()).getResources().getDrawable(R.drawable.icon_camera);
		}
		return drawable;
	}

	void reloadData() {
		indexes.clear();
		List<Setting> systemFolders = new ArrayList<Setting>();
		List<Setting> customFolders = new ArrayList<Setting>();
		for (int i = 0; i < folders.length(); i++) {
			JSONObject folder = folders.optJSONObject(i);

			// 5-18
			// Drawable drawable = ((ACTIVITY)
			// getActivity()).getResources().getDrawable(R.drawable.icon_camera);
			// Drawable drawable = ((ACTIVITY) getActivity()).getAssets().

			boolean isSystem = folder.optBoolean("system");
			Setting setting = new Setting(geticon(folder), folder.optString("name"), !isSystem,
					folder.optBoolean("hidden"));

			if (isSystem) {
				indexes.put(0 + "," + systemFolders.size(), i);
				systemFolders.add(setting);
			} else {
				indexes.put(1 + "," + customFolders.size(), i);
				customFolders.add(setting);
			}
		}

		// settingGroups = new SettingGroup[] { new SettingGroup(1, "系统文件夹",
		// systemFolders),
		// new SettingGroup(2, "自定义文件夹", customFolders) };
		settingGroups = new SettingGroup[] { new SettingGroup(1, "系统文件夹", systemFolders),
				new SettingGroup(3, "自定义文件夹", customFolders) };

		// 3个回调函数.分别对应 修改/隐藏/删除 3个事件
		settingListView.setData(settingGroups, new CALLBACK<int[]>() {

			@Override
			public void run(boolean isError, int[] result, View view) {
				if (isError) {
					return;
				}
				final JSONObject folder = folders.optJSONObject(getIndex(result));
				DIALOG.input("请输入新名称?", new CALLBACK<String>() {

					@Override
					public void run(boolean isError, String result) {
						if (isError) {
							return;
						}
						try {
							folder.put("name", result);
							CONFIG.set("folders", folders);
							reloadData();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

				});
			}
		}, new CALLBACK<int[]>() {

			@Override
			public void run(boolean isError, int[] result, View view) {
				if (isError) {
					return;
				}
				final JSONObject folder = folders.optJSONObject(getIndex(result));

				final boolean hidden = folder.optBoolean("hidden");
				String msg;
				if (hidden) {

					msg = "已经隐藏，是否设置显示？";
				} else {
					msg = "已经显示，是否设置隐藏？";
				}
				try {
					DIALOG.confirm(msg, new CALLBACK<Object>() {

						@Override
						public void run(boolean isError, Object result) {
							if (isError) {
								return;
							}
							try {

								folder.put("hidden", !hidden);
								CONFIG.set("folders", folders);
								reloadData();

								DIALOG.toast("设置成功");
							} catch (Exception e) {
								e.printStackTrace();
							}
						}

					}, new CALLBACK<Object>() {
						public void run(boolean isError, Object result) {
						};
					});

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
			/*
			 * try { JSONObject folder =
			 * folders.optJSONObject(getIndex(result)); boolean hidden =
			 * folder.optBoolean("hidden"); folder.put("hidden", !hidden);
			 * CONFIG.set("folders", folders); reloadData(); } catch (Exception
			 * e) { e.printStackTrace(); }
			 */

			// }
		}, new CALLBACK<int[]>() {

			@Override
			public void run(boolean isError, int[] result, View view) {
				if (isError) {
					return;
				}
				final JSONObject folder = folders.optJSONObject(getIndex(result));
				try {
					DIALOG.confirm("是否删除?", new CALLBACK<Object>() {

						@Override
						public void run(boolean isError, Object result) {
							if (isError) {
								return;
							}
							try {
								List<Object> settingListView = new ArrayList<Object>();
								for (int i = 0; i < folders.length(); i++) {
									settingListView.add(folders.get(i));
									JSONObject gardenJson = (JSONObject) folders.get(i);
									if (folder.equals(gardenJson)) {
										settingListView.remove(i);
									}
								}
								folders = new JSONArray(settingListView);
								CONFIG.set("folders", folders);
								reloadData();
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}

					}, new CALLBACK<Object>() {
						public void run(boolean isError, Object result) {
						};
					});

				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			/*
			 * @Override public void run(boolean isError, int[] result) { if
			 * (isError) { return; } try { folders.remove(getIndex(result));
			 * CONFIG.set("folders", folders); reloadData(); } catch (Exception
			 * e) { e.printStackTrace(); } }
			 */
		});
	}

	int getIndex(int[] result) {
		return indexes.get(result[0] + "," + result[1]);
	}
}
